/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Classify.java
 *
 * Created on 14/10/2010, 11:19:49
 */

package view.jdialog;

import controller.FacadeHost;
import interfaces.Base;
import interfaces.mining.classify.ClassifierModuleInterface;
import javax.swing.JTable;

/**
 *
 * @author Fernando
 */
public class JDialogClassify extends javax.swing.JDialog {
    Base base;
    ClassifierModuleInterface classifyModule;

    /** Creates new form Classify */
    public JDialogClassify(java.awt.Frame parent, boolean modal, Base base, ClassifierModuleInterface classifyModule) {
        super(parent, modal);
        this.base = base;
        this.classifyModule = classifyModule;
        initComponents();
        loadTable();  
        
        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-getWidth())/2, (screenSize.height-getHeight())/2, getWidth(), getHeight());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButtonClassify = new javax.swing.JButton();
        jButtonStore = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(jScrollPane1, gridBagConstraints);

        jButtonClassify.setText("Classificar");
        jButtonClassify.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonClassifyActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 0.95;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 5);
        getContentPane().add(jButtonClassify, gridBagConstraints);

        jButtonStore.setText("Armazenar");
        jButtonStore.setEnabled(false);
        jButtonStore.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonStoreActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.05;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 5);
        getContentPane().add(jButtonStore, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonClassifyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonClassifyActionPerformed
        // TODO add your handling code here:
        jButtonClassifyActionPerformed();
    }//GEN-LAST:event_jButtonClassifyActionPerformed

    private void jButtonStoreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonStoreActionPerformed
        // TODO add your handling code here:
        jButtonStoreActionPerformed();
    }//GEN-LAST:event_jButtonStoreActionPerformed

//    /**
//    * @param args the command line arguments
//    */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                Classify dialog = new Classify(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonClassify;
    private javax.swing.JButton jButtonStore;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    /**
     * action performed by the jbuttonclassify button
     */
    private void jButtonClassifyActionPerformed() {
        Object []saida = new Object[base.getInput().length];
        for (int i = 0; i < base.getInput().length; i++) {
            saida[i] = classifyModule.test(base.getInput()[i]);            
        }
        for (int i= 0; i < saida.length; i++) {
            jTable1.setValueAt(saida[i], i, base.getAtributes().length-1);
        }       
       jButtonStore.setEnabled(true);
    }

    /**
     * action performed by the jbuttonstore button
     */
    private void jButtonStoreActionPerformed() {
        Base newBase = new Base();
        newBase.setAtributes(base.getAtributes().clone());
        
        Object[][] data = new Object[jTable1.getRowCount()][jTable1.getColumnCount()-1];
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                data[i][j] = jTable1.getValueAt(i, j);                
            }
        }
        newBase.setInput(data);
        Object []saida = new Object[jTable1.getRowCount()];
        for (int i = 0; i < saida.length; i++) {
            saida[i] = jTable1.getValueAt(i, jTable1.getColumnCount()-1);
        }
        newBase.setOutput(saida);

        FacadeHost.getHost().addBase(newBase);
        jButtonStore.setEnabled(false);
    }

    /**
     * loads the data in table
     */
    private void loadTable() {
        Object[][] data = new Object[base.getInput().length][base.getAtributes().length];
        for (int i= 0; i < base.getInput().length; i++) {
            for (int j = 0; j < base.getInput()[i].length; j++) {
                data[i][j] = base.getInput()[i][j];
            }
        }
        for (int i= 0; i < base.getInput().length; i++) {
            data[i][base.getInput()[0].length] = base.getOutput()[i];
        }
        jTable1.setModel(new javax.swing.table.DefaultTableModel(data,base.getAtributes()));
    }

}
